package com.hsy.bibc.application;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.deparse.dpframe.ui.application.DPBaseApplication;
import com.deparse.dpframe.utils.DPLogUtil;
import com.deparse.dpframe.utils.DPPicassoUtil;
import com.hsy.bibc.common.Consts;
import com.hsy.bibc.R;
import com.hsy.bibc.utils.HttpUtil;
import com.tencent.android.tpush.XGPushManager;


import cn.sharesdk.framework.ShareSDK;
import im.fir.sdk.FIR;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Message;

/**
 * Created by hsy on 2016/4/7.
 */
public class Application extends DPBaseApplication {
    static {
        DPLogUtil.setDebugEnabled(true);
    }

    private static Application mInstance;

    public static Application getInstance() {
        return mInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        String processName = getProcessName(getApplicationContext());
        DPLogUtil.d("processName=" + processName);
        if (null != getPackageInfo() && getPackageInfo().packageName.equals(processName)) {
            mInstance = this;
            DPPicassoUtil.setErrorImageRes(R.drawable.err_image);
            HttpUtil.init(getApplicationContext());
//            startService(new Intent("UploadService"));
            ShareSDK.initSDK(this);
            FIR.init(this);


//            XGPushManager.registerPush(LoginActivity.this, new XGIOperateCallback() {
//                @Override
//                public void onSuccess(Object o, int i) {
//                    DPLogUtil.d("registerPush.onSuccess");
//                }
//
//                @Override
//                public void onFail(Object o, int i, String s) {
//                    DPLogUtil.d("registerPush.onFail");
//                }
//            });


//            RongIM.setOnReceiveMessageListener(new RongIMClient.OnReceiveMessageListener() {
//                @Override
//                public boolean onReceived(Message message, int i) {
//                    DPLogUtil.d("onReceive senderUserId=" + message.getSenderUserId());
//                    Intent intent = new Intent(Consts.Actions.ACTION_ON_RONG_MESSAGE_RECEIVED);
//                    sendBroadcast(intent);
//                    return false;
//                }
//            });

        }
    }

    private PackageInfo getPackageInfo() {
        PackageInfo info = null;
        try {
            info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
//            // 当前应用的版本名称
//            String versionName = info.versionName;
//            // 当前版本的版本号
//            int versionCode = info.versionCode;
//            // 当前版本的包名
//            String packageNames = info.packageName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return info;
    }


    @Override
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }
}
